package edu.gdin.ecommerce.action;


import java.util.Map;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

import edu.gdin.ecommerce.model.Account;
import edu.gdin.ecommerce.model.Card;
import edu.gdin.ecommerce.model.Inventory;
import edu.gdin.ecommerce.model.User;
import edu.gdin.ecommerce.service.IAccountService;
import edu.gdin.ecommerce.service.ICardService;
import edu.gdin.ecommerce.service.IInventoryService;
import edu.gdin.ecommerce.util.DateUtil;
import edu.gdin.ecommerce.util.DicConverter;
import edu.gdin.ecommerce.util.Pager;


@Component("cardAction")
@Scope("prototype")
public class CardAction extends ActionSupport {

	private Integer accountId;
	private Pager pager;
	private Integer currentPage;
	private Account account;
	private String msg;
	private Integer cardId;
	private String passwd;
	private Integer status;
	private Double creditAmount;
	
	//添加副卡
	private String inventoryNo;
	private Card card;
	
	private String result;
	
	private ICardService cardService;
	private IAccountService accountService;
	private IInventoryService inventoryService;
	
	public Integer getAccountId() {
		return accountId;
	}

	public void setAccountId(Integer accountId) {
		this.accountId = accountId;
	}
	
	public ICardService getCardService() {
		return cardService;
	}

	@Resource
	public void setCardService(ICardService cardService) {
		this.cardService = cardService;
	}

	public Integer getCurrentPage() {
		return currentPage;
	}

	public void setCurrentPage(Integer currentPage) {
		this.currentPage = currentPage;
	}

	public Pager getPager() {
		return pager;
	}

	public void setPager(Pager pager) {
		this.pager = pager;
	}
	
	public IAccountService getAccountService() {
		return accountService;
	}

	@Resource
	public void setAccountService(IAccountService accountService) {
		this.accountService = accountService;
	}
	
	public Account getAccount() {
		return account;
	}

	public void setAccount(Account account) {
		this.account = account;
	}

	
	public Integer getCardId() {
		return cardId;
	}

	public void setCardId(Integer cardId) {
		this.cardId = cardId;
	}

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}
	
	public String getResult() {
		return result;
	}

	public void setResult(String result) {
		this.result = result;
	}
	

	public String getPasswd() {
		return passwd;
	}

	public void setPasswd(String passwd) {
		this.passwd = passwd;
	}

	
	public Integer getStatus() {
		return status;
	}

	public void setStatus(Integer status) {
		this.status = status;
	}

	public Double getCreidtAmount() {
		return creditAmount;
	}

	public void setCreidtAmount(Double creidtAmount) {
		this.creditAmount = creidtAmount;
	}
	

	public String getInventoryNo() {
		return inventoryNo;
	}

	
	public void setInventoryNo(String inventoryNo) {
		this.inventoryNo = inventoryNo;
	}

	public IInventoryService getInventoryService() {
		return inventoryService;
	}
	
	@Resource
	public void setInventoryService(IInventoryService inventoryService) {
		this.inventoryService = inventoryService;
	}

	public Card getCard() {
		return card;
	}

	public void setCard(Card card) {
		this.card = card;
	}

	public String getCardByAccountIdAction(){
		Integer cpg = currentPage;
		if( currentPage==null ||currentPage==0 )
			cpg = 1;
		setAccount(accountService.getAccountById(accountId));
		setPager(cardService.getCardPagerByAccountId(accountId, cpg));
		return "cardList";
	}
	
	public String getCardRecordAction(){
		Integer cpg = currentPage;
		if( currentPage==null ||currentPage==0 )
			cpg = 1;
		setAccount(accountService.getAccountById(accountId));
		setPager(cardService.getCardRecoredAccountId(accountId, cpg));
		if(pager.getList().size()==0)
			this.addFieldError("msg", "暂无数据！");
		return "recordList";
	}
	
	public String resetPasswd(){
		if(this.cardService.resetCardPasswd(cardId, passwd))
			result = "success";
		else result = "faild";
		
		return SUCCESS;
	}
	
	public String updateStatus(){
		if(status==1){
			Account acc = accountService.getAccountById(accountId);
			if(acc.getStatus()!=1 || !acc.getCustomer().getCertificateNo().equals("00000000000000000")){
				result = "abnormal";
			}
		}else{
			if(cardService.updateStatus(cardId, status))
				result = "success";
			else result = "faild";
		}
		return SUCCESS;
	}
	
	public String addAttachCard(){
		Map<String, Object> session = (Map<String, Object>) ActionContext.getContext().getSession();
		User user = (User) session.get("user");
		
		Inventory inventory  = inventoryService.queryInvenoryById(inventoryNo);
		Account account = accountService.getAccountById(accountId);
		Card c = new Card();
		c.setCardNo(inventory.getInventoryNo());
		c.setCardDesc(inventory.getInventoryDesc());
		c.setCardIssuer(inventory.getCardIssuer());
		c.setCreateTime(DateUtil.now());
		c.setCreateUser(user.getUsername());
		c.setMaterialType(inventory.getMaterialType());
		c.setPassword(inventory.getPassword());
		c.setUseType(card.getUseType());
		c.setBatchNo(inventory.getBatchNo());
		c.setSubCardExpense(card.getSubCardExpense());
		c.setCardDesc(card.getCardDesc());
		c.setBatchNo(inventory.getBatchNo());
		c.setStatus(2);
		c.setMark(2);
		c.setAccount(account);
		
		
		if(this.cardService.addCard(c)){
			inventory.setStatus(4);
			if(this.inventoryService.updateInvenory(inventory)){
				setMsg("success");
				return "attachAdd";
			}
		}
		this.addFieldError("msg","添加出错，请稍后再试！");
		return "attachAdd";
	}
	
	public String toAttachAddPage(){
		return "attachAdd";
	}
}
